package com.zzh.service.impl;

import com.zzh.mapper.ItemRelationMapper;
import com.zzh.model.ItemRelation;
import com.zzh.service.ItemRelationServiceImpl;
import com.zzh.util.BaseResultInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Supplier;

/**
 * @author：zhangzhanheng
 * @createTime: 2021/9/3  10:53 上午
 * @desc:
 * @version: 1.0.0
 **/
@Service
public class ItemRelationService implements ItemRelationServiceImpl {
    @Resource
    private ItemRelationMapper itemRelationMapper;

    private static final   ExecutorService executor = new ThreadPoolExecutor(
            5,
            20,
            3000,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(5));

    @Override
    public BaseResultInfo updateStatus() {
        List<ItemRelation> itemRelations = itemRelationMapper.selectList(null);

        System.out.println("main函数开始执行");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                System.out.println("===task start===");
                for (ItemRelation itemRelation : itemRelations) {
                    itemRelation.setStatus(10);
                    itemRelationMapper.updateById(itemRelation);
                }
                System.out.println("===task finish===");
                return 3;
            }
        }, executor);
        future.thenAccept(e -> System.out.println(e));
        System.out.println("main函数执行结束");
        return BaseResultInfo.successResult("执行结束");
    }

    @Override
    public BaseResultInfo add() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                System.out.println("===task start===");
                for (int i = 0; i < 1000; i++) {
                    for (int j = 0; j < 1000; j++) {
                        ItemRelation itemRelation = new ItemRelation();
                        itemRelation.setAppId(i+j+"");
                        itemRelation.setStatus(1);
                        itemRelation.setCreateTime(LocalDateTime.now());
                        itemRelationMapper.insert(itemRelation);
                    }
                }
                System.out.println("===task finish===");
                return 3;
            }
        }, executor);
        return BaseResultInfo.successResult("创建sql");
    }
}
